// 最长回文子串  连续的  s = "babad" =》 bab aba
/**
 * @param {string} s
 * @return {string}
 */
var longestPalindrome = function (s) {
  let maxStr = ''

  let len = s.length
  let dp = new Array(len)
  for (let i = 0; i < len; i++) {
    dp[i] = new Array(len).fill(0)
    dp[i][i] = 1
    maxStr = s[i] // 初始化最长回文子串为单个字符
  }

  for (let i = len - 1; i >= 0; i--) {
    for (let j = i + 1; j < len; j++) {
      if (s[i] === s[j]) {
        // 判断里面有几个
        if (j - i === 1 || dp[i + 1][j - 1] > 0) {
          dp[i][j] = dp[i + 1][j - 1] + 2
          if (dp[i][j] > maxStr.length) {
            maxStr = s.substring(i, j + 1)
          }
        }
      }
    }
  }
  return maxStr
};

console.log(longestPalindrome('babad'));

